/*
 * \brief  User state implementation
 * \author Norman Feske
 * \date   2006-08-27
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#include <input/event.h>
#include <input/keycodes.h>
#include "user_state.h"

using namespace Input;

/*
 * Definition of magic keys
 */
enum { KILL_KEY = KEY_PRINT      };
enum { XRAY_KEY = KEY_SCROLLLOCK };


/***************
 ** Utilities **
 ***************/

static inline bool _masked_key(int keycode) {
	return keycode == KILL_KEY || keycode == XRAY_KEY; }


static inline bool _mouse_button(int keycode) {
		return keycode >= BTN_LEFT && keycode <= BTN_MIDDLE; }


/**************************
 ** User state interface **
 **************************/

User_state::User_state(Canvas *canvas, Menubar *menubar):
	View_stack(canvas, this), _key_cnt(0), _menubar(menubar) { }


void User_state::handle_event(Input::Event ev)
{
	/*
	 * Mangle incoming events
	 */

	int keycode = ev.keycode();
	int ax = _mouse_pos.x(), ay = _mouse_pos.y();

	/* KEY_PRINT and KEY_SYSRQ both enter kill mode */
	if ((ev.type() == Event::PRESS) && (ev.keycode() == KEY_SYSRQ))
		keycode = KEY_PRINT;

	/* transparently handle absolute and relative motion events */
	if (ev.type() == Event::MOTION) {
		if ((ev.rx() || ev.ry()) && ev.ax() == 0 && ev.ay() == 0) {
			ax = max(0, min(size().w(), ax + ev.rx()));
			ay = max(0, min(size().h(), ay + ev.ry()));
		} else {
			ax = ev.ax();
			ay = ev.ay();
		}
	}

	/* create the mangled event - we skip info about relative motion */
	ev = Input::Event(ev.type(), keycode, ax, ay, 0, 0);

	_mouse_pos = Point(ax, ay);

	View *pointed_view = find_view(_mouse_pos);

	/*
	 * We expect pointed view to be always defined. In the worst case (with no
	 * view at all), the pointed view is the background.
	 */

	bool update_all = false;

	if (ev.type() == Event::PRESS && _key_cnt == 0) {

		/* update focused view */
		if (pointed_view != focused_view()
		 && _mouse_button(ev.keycode())) {

			_focused_view = pointed_view;
			update_all = true;
		}

		/* toggle kill and xray modes */
		if (ev.keycode() == KILL_KEY || ev.keycode() == XRAY_KEY) {

			Mode::_mode ^= ev.keycode() == KILL_KEY ? KILL : XRAY;
			update_all = true;
		}
	}

	if (update_all) {

		if (focused_view() && focused_view()->session())
			_menubar->state(*this, focused_view()->session()->label(),
			 focused_view()->title());
		else
			_menubar->state(*this, "", "");

		update_all_views();
	}

	/* count keys */
	if (ev.type() == Event::PRESS) _key_cnt++;
	if (ev.type() == Event::RELEASE && _key_cnt > 0) _key_cnt--;

	/*
	 * Deliver event to Nitpicker session.
	 * (except when kill mode is activated)
	 */

	if (kill()) return;

	if (ev.type() == Event::MOTION || ev.type() == Event::WHEEL) {

		if (_key_cnt == 0) {

			/*
			 * In flat mode, we deliver motion events to the session of
			 * the pointed view. In xray mode, we deliver motion
			 * events only to the session with the focused view.
			 */
			if (flat() || (xray() && focused_view() == pointed_view))
				if (pointed_view)
					pointed_view->session()->submit_input_event(&ev);

		} else if (focused_view())
			focused_view()->session()->submit_input_event(&ev);
	}

	/* deliver press/release event to session with focused view */
	if (ev.type() == Event::PRESS || ev.type() == Event::RELEASE)
		if (!_masked_key(ev.keycode()) && focused_view())
			focused_view()->session()->submit_input_event(&ev);
}


/********************
 ** Mode interface **
 ********************/

void User_state::forget(View *v)
{
	if (_focused_view == v) {
		Mode::forget(v);
		_menubar->state(*this, "", "");
		update_all_views();
	}
}

